package com.example.netty;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by dingshuo on 2017/7/6.
 */
public class UploadServer {
    public void bind(int port) throws Exception{
        EventLoopGroup bossGroup=new NioEventLoopGroup();
        EventLoopGroup workerGroup=new NioEventLoopGroup();
        try{
            ServerBootstrap b=new ServerBootstrap();
            b.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,1024)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                                @Override
                                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                    super.channelActive(ctx);
                                }

                                @Override
                                public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                                    super.channelInactive(ctx);
                                }

                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    String path="D:\\data\\aa1.mp4";
                                    File file=new File(path);
                                    if(!file.exists()){
                                        file.createNewFile();
                                    }

                                    FileOutputStream fos=new FileOutputStream(file,true);
//                                    BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fos);
                                    ByteBuf buf=(ByteBuf)msg;

                                    byte[] bytes=new byte[buf.readableBytes()];
                                    buf.readBytes(bytes);
                                    System.out.println("本次接收内容长度：" + bytes.length);
                                    try {
//                                        bufferedOutputStream.write(bytes, 0, bytes.length);
//                                        buf.release();
                                        fos.write(bytes);
                                        fos.flush();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    });

            //绑定端口，同步等待成功
            ChannelFuture f=b.bind(port).sync();

            //等待服务端监听端口关闭
            f.channel().closeFuture().sync();
        }finally {
            //退出，释放资源
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception{
        UploadServer uploadServer=new UploadServer();
        uploadServer.bind(20000);
    }
}
